package com.ruicar.afs.cloud.afscase.channel.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.condition.ChannelVeicleCondition;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAuthorizeVehicle;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelRiskInfo;
import com.ruicar.afs.cloud.afscase.channel.enums.VehicleEnum;
import com.ruicar.afs.cloud.afscase.channel.enums.WhetherEnum;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAuthorizeVehicleService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelRiskInfoService;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelBrandVo;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelModelVo;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelVehicleBrandVo;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelVehicleModelVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.parameter.commom.vo.VehicleModelVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 授权车型
 * @author: mingzhi
 * @date: 2020/9/10 14:03
 */
@Slf4j
@RestController
@RequestMapping(value = "/caseVehicle")
public class ChannelVehicleController {

    @Autowired
    private ChannelAuthorizeVehicleService channelAuthorizeVehicleService;
    @Autowired
    private ChannelRiskInfoService channelRiskInfoService;
    /**
     * 查询品牌列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getBrandList")
    @ApiOperation("查询品牌列表")
    public IResponse<List<ChannelVehicleBrandVo>> getBrandList(@RequestBody ChannelVeicleCondition condition) {

        if(StrUtil.isBlank(condition.getLevel())){
            return IResponse.fail("层级不能为空");
        }
        if(condition.getChannelId()==null){
            return IResponse.fail("渠道id为空");
        }
        if(StrUtil.isBlank(condition.getBusinessType())){
            return IResponse.fail("业务类型不能为空");
        }
        ChannelBrandVo brandVo = new ChannelBrandVo();
        ChannelAuthorizeVehicle vehicle = channelAuthorizeVehicleService.getOne(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                .eq(ChannelAuthorizeVehicle::getCarLevel, VehicleEnum.ZERO.getCode())
                .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType()));
        //品牌全选情况
        if(vehicle !=null && "false".equals(vehicle.getIndeterminate())){
            brandVo.setAuthVehicleTypeSwitch(WhetherEnum.NO.getCode());
            return IResponse.success(brandVo);
        }

        ChannelRiskInfo riskInfo = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda()
                .eq(ChannelRiskInfo::getChannelId, condition.getChannelId().toString())
                .eq(ChannelRiskInfo::getBusinessType, condition.getBusinessType()));
        if(riskInfo !=null && WhetherEnum.YES.getCode().equals(riskInfo.getAuthVehicleTypeSwitch())){
            //授权车型是否生效0否，1是
            brandVo.setAuthVehicleTypeSwitch(WhetherEnum.YES.getCode());
        }else{
            brandVo.setAuthVehicleTypeSwitch(WhetherEnum.NO.getCode());
        }
        List<ChannelAuthorizeVehicle> vehicleList = channelAuthorizeVehicleService.list(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                        .eq(ChannelAuthorizeVehicle::getCarLevel, condition.getLevel())
                        .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                        .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType())
                        .eq(StringUtils.isNotBlank(condition.getCarType()),ChannelAuthorizeVehicle::getCarType,condition.getCarType()).orderByAsc(ChannelAuthorizeVehicle::getSpellCode));
        List<ChannelVehicleBrandVo> resultList = new ArrayList<ChannelVehicleBrandVo>();
        if(vehicleList !=null && vehicleList.size()>0){
            for (ChannelAuthorizeVehicle brand : vehicleList) {
                ChannelVehicleBrandVo vo = new ChannelVehicleBrandVo();
                vo.setBrandId(brand.getCode());
                vo.setBrandName(brand.getTitle());
                vo.setIndeterminate(brand.getIndeterminate());
                Character firstChar = Character.toUpperCase(brand.getSpellCode().charAt(0));
                vo.setFirstChar(firstChar);
                resultList.add(vo);
            }
        }
        brandVo.setResultList(resultList);
        return IResponse.success(brandVo);
    }

    /**
     * 查询车型列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getModelList")
    @ApiOperation("查询车型列表")
    public IResponse<List<VehicleModelVO>> getModelList(@RequestBody ChannelVeicleCondition condition) {

        if(StrUtil.isBlank(condition.getBrandId())){
            return IResponse.fail("品牌id不能为空");
        }
        if(StrUtil.isBlank(condition.getLevel())){
            return IResponse.fail("层级不能为空");
        }
        if(condition.getChannelId()==null){
            return IResponse.fail("渠道id为空");
        }
        if(StrUtil.isBlank(condition.getBusinessType())){
            return IResponse.fail("业务类型不能为空");
        }
        //step1: 根据品牌id、渠道id、业务类型获取当前品牌详细信息
        ChannelAuthorizeVehicle brand = channelAuthorizeVehicleService.getOne(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                .eq(ChannelAuthorizeVehicle::getCode, condition.getBrandId())
                .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType()));

        //step1: 根据品牌id、渠道id、业务类型获取当前品牌子集
        List<ChannelAuthorizeVehicle> modelList = channelAuthorizeVehicleService.list(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                .eq(ChannelAuthorizeVehicle::getCarLevel, condition.getLevel())
                .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType())
                .eq(ChannelAuthorizeVehicle::getParentId,condition.getBrandId())
                .like(StringUtils.isNotBlank(condition.getModelName()), ChannelAuthorizeVehicle::getTitle, condition.getModelName()));

        ChannelRiskInfo riskInfo = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda()
                .eq(ChannelRiskInfo::getChannelId, condition.getChannelId().toString())
                .eq(ChannelRiskInfo::getBusinessType, condition.getBusinessType()));
        ChannelModelVo modelVo = new ChannelModelVo();
        if(riskInfo !=null){
            //授权车型是否生效0否，1是
            modelVo.setAuthVehicleTypeSwitch(WhetherEnum.YES.getCode());
        }else{
            modelVo.setAuthVehicleTypeSwitch(WhetherEnum.NO.getCode());
        }
        List<ChannelVehicleModelVo> resultList = new ArrayList<ChannelVehicleModelVo>();
        for (ChannelAuthorizeVehicle model : modelList) {
            ChannelVehicleModelVo vo = new ChannelVehicleModelVo();
            vo.setBrandId(brand.getCode());
            vo.setModelId(model.getCode());
            vo.setModelName(model.getTitle());
            Character firstChar = Character.toUpperCase(brand.getSpellCode().charAt(0));
            vo.setFirstChar(firstChar);
            vo.setBrandName(brand.getTitle());
            resultList.add(vo);
        }
        modelVo.setResultList(resultList);
        return IResponse.success(modelVo);
    }

    /**
     * 查询车型列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getModeTitleList")
    @ApiOperation("查询车型列表下拉查询")
    public IResponse<List<VehicleModelVO>> getModeTitleList(@RequestBody ChannelVeicleCondition condition) {

        if(StrUtil.isBlank(condition.getLevel())){
            return IResponse.fail("层级不能为空");
        }
        if(condition.getChannelId()==null){
            return IResponse.fail("渠道id为空");
        }
        if(StrUtil.isBlank(condition.getBusinessType())){
            return IResponse.fail("业务类型不能为空");
        }
        ChannelModelVo modelVo = new ChannelModelVo();
        List<ChannelVehicleModelVo> resultList = new ArrayList<ChannelVehicleModelVo>();
        //step1: 根据品牌id、渠道id、业务类型获取当前品牌详细信息
        List<ChannelAuthorizeVehicle> modelList = channelAuthorizeVehicleService.list(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                .like(StringUtils.isNotBlank(condition.getModelName()), ChannelAuthorizeVehicle::getTitle, condition.getModelName())
                .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType())
                .eq(ChannelAuthorizeVehicle::getCarLevel,condition.getLevel()));
        if(modelList !=null && modelList.size()>0){
            for(ChannelAuthorizeVehicle vehicle :modelList){
                //获取车型的上级：品牌信息
                ChannelAuthorizeVehicle brand = channelAuthorizeVehicleService.getOne(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                        .eq(ChannelAuthorizeVehicle::getCode, vehicle.getParentId())
                        .eq(ChannelAuthorizeVehicle::getChannelId,condition.getChannelId())
                        .eq(ChannelAuthorizeVehicle::getBusinessType,condition.getBusinessType()));
                ChannelVehicleModelVo vo = new ChannelVehicleModelVo();
                vo.setModelId(vehicle.getCode());
                vo.setModelName(vehicle.getTitle());
                vo.setBrandId(brand.getCode());
                Character firstChar = Character.toUpperCase(brand.getSpellCode().charAt(0));
                vo.setFirstChar(firstChar);
                vo.setBrandName(brand.getTitle());
                resultList.add(vo);
            }
            modelVo.setAuthVehicleTypeSwitch(WhetherEnum.YES.getCode());
            modelVo.setResultList(resultList);
            return IResponse.success(modelVo);
        }else{
            modelVo.setAuthVehicleTypeSwitch(WhetherEnum.YES.getCode());
            modelVo.setResultList(resultList);
            return IResponse.success(modelVo);
        }
    }
}
